<?php
	// Validation for rosters
	function validate_roster_data($members, $data) {
		$errmsg = '';
		
		$roster_db = file_get_contents('tweaks/roster/rosterdata.tdb');
		$roster_db = unserialize($roster_db);
		
		// Format the roster name.
		$data['name'] = ucwords(strtolower($data['name']));
		
		// Name Validation
		if (!ctype_alpha($data['name']) || (strlen($data['name']) > 12) || empty($data['name'])) {		
			// Invalid roster name.
			$errmsg = $errmsg . 'Invalid Roster Name<br />';
		}
		
		// Check if the roster exists for new rosters only.
		if (array_key_exists($data['name'], $roster_db) && $_GET['r'] == 'New') {
			// Roster name exists.
			$errmsg = $errmsg . 'Roster Name Exists<br />';
		}
		
		// Roster Size Validation
		if (count($_SESSION['newroster']) < $data['size']) {
			// Less people in the roster than the raid capacity
			$errmsg = $errmsg . 'Need more people or change Raid Size.<br />';
		}

		// error message empty, save the file
		if (empty($errmsg)) {
			$roster_db[$data['name']] = $data;
			
			// Get the members from the session array.
			foreach ($members as $mem) {
				if (in_array($mem['name'], $_SESSION['newroster'])) {
					$roster_db[$data['name']]['members'][] = $mem;
				}
			}
			
			// Save the rosters to the file.
			$roster_db = serialize($roster_db);
			file_put_contents('tweaks/roster/rosterdata.tdb', $roster_db);
			
			// Clear the Session and Post arrays.
			unset($_SESSION['newroster']);
			unset($_POST);
			
			// Create the members page if it doesnt' exist.
			if (!file_exists('pages/rosters.php')) {
				$rosters_page = "<!-- Page auto-generated -->\n";
				$rosters_page .= "<?php runTweak('rosters-page'); ?>";
			
				file_put_contents('pages/rosters.php', $rosters_page);
					
				update_nanocms_pages('rosters');
			}
		}
		
		return $errmsg;
	}
	
	// UI for creating/editing rosters.
	function roster_ui($members, $data) {
		$sel = ' selected="selected"';
	
		// Raid Type
		switch ($data['raidtype']) {
			case 'Environment':
				$tEnv = $sel;
				break;
			case 'Player':
				$tPla = $sel;
				break;
		}
		
		// Raid Size
		switch ($data['size']) {
			case 10:
				$s10 = $sel;
				break;
			case 15:
				$s15 = $sel;
				break;
			case 20:
				$s20 = $sel;
				break;
			case 25:
				$s25 = $sel;
				break;
			case 40:
				$s40 = $sel;
				break;
		}
		
		// Reserves
		switch ($data['reserves']) {
			case 'Yes':
				$rYes = $sel;
				break;
			case 'No':
				$rNo = $sel;
				break;
		}
		
		$roster_page = '<script type="text/javascript" src="tweaks/roster/roster.js"></script>';
		
		$roster_page .= '<object>
				<ul>
					<li><p><a href="?tweak=roster-tweak&r=Add">Add Member</a></p></li>
					<li><p><a href="?tweak=roster-tweak&r=Modify">Modify Members</a></p></li>
					<li>
						<p><a href="?tweak=roster-tweak&r=New">New Roster</a></p>
						<form name="roster" action="" method="post">
							<table>
								<tr>
									<th>Name : </th>
									<td><input type="text" name="name" value="' . $data['name'] . '" /></td>
									<td></td>
									<th>Player Vs</th>
									<td>
										<select name="raidtype">
											<option value="">Select Type</option>'."
											<option$tEnv>Environment</option>
											<option$tPla>Player</option>".'
										</select>
									</td>
								</tr>
								<tr>
									<th>Size : </th>
									<td>
										<select name="size" />
											<option value="">Select Size</option>'."
											<option$s10>10</option>
											<option$s15>15</option>
											<option$s20>20</option>
											<option$s25>25</option>
											<option$s40>40</option>".'
										</select>
									</td>
									<td></td>
									<th>Display Reserves : </th>
									<td>
										<select name="reserves">'."
											<option$rYes>Yes</option>
											<option$rNo>No</option>".'
										</select>
									</td>
								</tr>
								<tr>
									<th colspan="2">Roster</th>
									<td></td>
									<th colspan="2">Guild Members</th>
								</tr>
								<tr>
									<td colspan="2">
										<select size="10" name="roster[]" id="roster" multiple="multiple" style="width: 100%">';
											if (isset($_SESSION['newroster'])) {
												foreach ($_SESSION['newroster'] as $noobie) {
													$roster_page .= '<option>' . $noobie . '</option>';
												}
											}
										$roster_page .= '</select>
									</td>
									<td>
										<input type="submit" name="submit" value="<- Add" /><br />
										<input type="submit" name="submit" value="Del ->" /><br />
									</td>
									<td colspan="2">
										<select size="10" name="members[]" id="members"" multiple="multiple" style="width: 100%">';
											foreach ($members as $mem) {
												// Echo out each name from the guild members array.
												$roster_page .= '<option>' . $mem['name'] . '</option>';
											}
										$roster_page .=  '</select>
									</td>
								</tr>
								<tr>
									<td><input type="submit" name="submit" value="Save" /></td>
								</tr>
							</table>
						</form>
					</li>
					<li><a href="?tweak=roster-tweak&r=Modify_Roster">Modify Rosters</a></li>
				</ul>
			</object>';
			echo $roster_page;
	}

	// UI for picking members to modify or delete.
	function modify_rosters_ui($rosters) {
		echo '<object>
					<ul>
						<li><p><a href="?tweak=roster-tweak&r=Add">Add Member</a></p></li>
						<li><p><a href="?tweak=roster-tweak&r=Modify">Modify Members</a></p></li>
						<li><p><a href="?tweak=roster-tweak&r=New">New Roster</a></p></li>
						<li>
							<p><a href="?tweak=roster-tweak&r=Modify_Roster">Modify Rosters</a></p>
							<table>
								<tr>
									<th>Name</th>
									<th>Size</th>
									<th>Players</th>
								</tr>';
								// Loop through the members array and print a row for each member.
								foreach ($rosters as $rost) {									
									echo '<tr>
											<td>'. $rost['name'] .'</td>
											<td>'. $rost['size'] .'</td>
											<td>'. count($rost['members']) .'</td>
											<td><a href="?tweak=roster-tweak&r=Modify_Roster&edit='. $rost['name'] .'">Modify</a></td>
											<td><a href="?tweak=roster-tweak&r=Modify_Roster&delete='. $rost['name'] .'">Delete</a></td>
										</tr>';
								}
							echo '</table>
						</li>
					</ul>
			</object>';
	}	
?>